package com.atguigu.study.test;

import org.springframework.core.DefaultParameterNameDiscoverer;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
/**
 * @auther zzyy
 * @create 2024-12-17 0:39
 * java中如何获得方法中的参数名
 * https://developer.aliyun.com/article/593043#:~:text=%E4%B8%8B%E9%9D%A2%E4%BB%8B%E7%BB%8D%E4%B8%80%E7%A7%8D%E6%96%B9%E6%B3%95%E8%8E%B7%E5%BE%97%E5%8F%82%E6%95%B0%E7%9C%9F%E5%AE%9E%E5%90%8D%E7%A7%B0%20DefaultParameterNameDiscoverer%20discover%20%3D%20new%20DefaultParameterNameDiscoverer%20%28%29%3B,String%20%5B%5D%20parameterNames%20%3D%20discover.getParameterNames%20%28method%29%3B%20%E5%85%B6%E4%B8%ADparameterNames%E5%B0%B1%E6%98%AF%E5%8F%82%E6%95%B0%E7%9A%84%E7%9C%9F%E5%AE%9E%E5%90%8D%E7%A7%B0%E3%80%82
 */
public class T2
{
    public int add(int x ,int y)
    {
        return x + y;
    }

    /**
     * 两种方法可以获得方法中的参数名而不再是arg0,arg1,arg2之类的
     *
     * @param args
     */
    public static void main(String[] args)
    {
        Method[] methods = T2.class.getDeclaredMethods();

        for (Method method : methods)
        {
            System.out.println(method.getName());
            if("add".equalsIgnoreCase(method.getName()))
            {
                System.out.println("--11111");
                DefaultParameterNameDiscoverer discover = new DefaultParameterNameDiscoverer();
                String[] parameterNames = discover.getParameterNames(method);
                System.out.println(parameterNames.length);

                System.out.println("--2222");
                // 获取方法的参数
                /*Parameter[] parameters = method.getParameters();
                for (Parameter parameter : parameters) {
                    // 输出参数名和类型
                    System.out.println("Parameter Type: " + parameter.getType().getName());
                    System.out.println("Parameter Name: " + parameter.getName());
                }*/

            }
        }
    }

}

